package com.zjw.zy.base;

import android.view.View;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/27 13:27
 */
public abstract class BaseViewHolder<DATA> extends RecyclerView.ViewHolder {

    protected DATA mData;

    protected int type;

    protected int position;

    protected BaseRecyclerAdapter.ItemListener<BaseViewHolder<DATA>> mListener;

    public BaseViewHolder(@NonNull View itemView) {
        super(itemView);

        onInit();
    }

    private void onInit() {

    }

    public void bindData(DATA data, int position) {
        mData = data;
        this.position = position;

        onBindData(mData);
        onBindListener();
    }

    /**
     * 原引用，小心点！
     */
    public DATA getData() {
        return mData;
    }

    public int getType() {
        return type;
    }

    public void bindListener(BaseRecyclerAdapter.ItemListener<BaseViewHolder<DATA>> listener) {
        mListener = listener;

        itemView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onItemClick(BaseViewHolder.this, v);
            }
        });

        itemView.setOnLongClickListener(v -> {
            if (mListener != null) {
                return mListener.onItemLongClick(BaseViewHolder.this, v);
            }
            return false;
        });
    }

    protected abstract void onBindListener();

    protected abstract void onBindData(DATA data);

    protected final <V extends View> V findViewById(@IdRes int id) {
        return itemView.findViewById(id);
    }

}
